package functional_programming;

import java.util.Optional;
import java.util.function.Consumer;
import java.util.stream.Stream;

public class Optionals {
    static void basics(Optional<String> optString) {
        if (optString.isPresent()) {
            System.out.println(optString.get());
        }else{
            System.out.println("none");
        }
    }

    static void ifPresent(Optional<String> optString) {
        optString.ifPresent(System.out::println);
    }
    static void orElse(Optional<String> optString) {
        System.out.println(optString.orElse("orElse"));
    }
    static void orElseGet(Optional<String> optString) {
        System.out.println(optString.orElseGet(()-> "orElseGet Object"));
    }
    static void orElseThrow(Optional<String> optString) {
        try {
            System.out.println(optString.orElseThrow(
                    () -> new Exception("Supplied")
            ));

        } catch (Exception e) {
            System.out.println("caught Supplied");
        }
    }
    static void test(String testName, Consumer<Optional<String>> cos) {
        System.out.println(" === " + testName + " === ");
        cos.accept(Stream.of("EPitdsfds").findFirst());
        cos.accept(Stream.<String>empty().findFirst());

    }
    public static void main(String[] args) {
        test("basis", Optionals::basics);
        test("ifPresent", Optionals::ifPresent);
        test("orElse", Optionals::orElse);
        test("orElseGet", Optionals::orElseGet);
        test("orElseThrow", Optionals::orElseThrow);

    }

}
